<?php

/**
 * This is the model class for table "{{sellercat}}".
 *
 * The followings are the available columns in table '{{sellercat}}':
 * @property string $cid
 * @property integer $parent_cid
 * @property integer $uid
 * @property string $name
 * @property string $pic_url
 * @property integer $sort_order
 * @property integer $created
 * @property integer $modified
 * @property string $type
 */
class Sellercat extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Sellercat the static model class
	 */
	public $top_session;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'taobao_sellercat';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('parent_cid, uid, sort_order, created, modified', 'numerical', 'integerOnly'=>true),
			array('name, type', 'length', 'max'=>50),
			array('pic_url', 'length', 'max'=>255),
			array('name', 'required'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('cid, parent_cid, uid, name, pic_url, sort_order, created, modified, type', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'cid' => 'Cid',
			'parent_cid' => '父类目编号',
			'uid' => 'Uid',
			'name' => '类目名称',
			'pic_url' => '链接图片URL地址',
			'sort_order' => '排序位置',
			'created' => 'Created',
			'modified' => 'Modified',
			'type' => 'Type',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('cid',$this->cid,true);
		$criteria->compare('parent_cid',$this->parent_cid);
		$criteria->compare('uid',$this->uid);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('pic_url',$this->pic_url,true);
		$criteria->compare('sort_order',$this->sort_order);
		$criteria->compare('created',$this->created);
		$criteria->compare('modified',$this->modified);
		$criteria->compare('type',$this->type,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	protected function beforeSave()	{
		parent::beforeSave();
			if($this->isNewRecord && $this->top_session) {
				$req = new SellercatsListAddRequest;
				$req->setName($this->name);
				$req->setPictUrl($this->pic_url);
				if($this->parent_cid) $req->setParentCid($this->parent_cid);
				if($this->sort_order) $req->setSortOrder($this->sort_order);
				$resp = Yii::app()->top->execute($req, $this->top_session);
				if(!$resp->seller_cat->cid) {
					Yii::app()->user->setFlash('result',$resp->msg);
					return false;
				}else{
					$this->cid = $resp->seller_cat->cid;
					$this->created = strtotime($resp->seller_cat->created);					
					return true;
				}
			}else if($this->top_session){
				$req = new SellercatsListUpdateRequest;
				$req->setCid($this->cid);
				$req->setName($this->name);
				$req->setPictUrl($this->pic_url);
				$req->setSortOrder($this->sort_order);
				$resp = Yii::app()->top->execute($req,$this->top_session);
				if(!$resp->seller_cat->cid) {
					Yii::app()->user->setFlash('result',$resp->msg);
					return false;
				}else{
					$this->modified = strtotime($resp->seller_cat->modified);					
					return true;
				}
			}
				
		return true;
	}

}